package com.jiangxiaofei.citycloud.activity.park;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.blankj.utilcode.util.EmptyUtils;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.ParkDetailInfoBean;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.CarConstant;
import com.jiangxiaofei.citycloud.mvp.CarPersenter;
import com.jiangxiaofei.citycloud.util.OpenLocalMapUtil;
import com.jiangxiaofei.citycloud.view.ProgressDialogHelper;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 作者: JiangXfei
 * 日期: 2017/9/22 0022
 */

public class ParkDetailActivity extends BaseActivity implements CarConstant.getParkDetailView {
    @InjectView(R.id.tv_park_detail_title)
    TextView tvParkDetailTitle;
    @InjectView(R.id.tv_pd_address)
    TextView tvPdAddress;
    @InjectView(R.id.tv_pd_distance)
    TextView tvPdDistance;
    @InjectView(R.id.tv_pd_price)
    TextView tvPdPrice;
    @InjectView(R.id.tv_pd_free_count)
    TextView tvPdFreeCount;
    @InjectView(R.id.tv_pd_price_content)
    TextView tvPdPriceContent;
    @InjectView(R.id.tv_pd_phone_number)
    TextView tvPdPhoneNumber;
    @InjectView(R.id.tv_pd_people)
    TextView tvPdPeople;
    @InjectView(R.id.btn_pd_navigation)
    Button btnPdNavigation;

    CarConstant.getParkDetailPersenter getParkDetailPersenter;
    private String parkSeq;
    private LatLng latLng;

    private static String SRC = "com.jiangxiaofei.CityCloud";
    private static String APP_NAME = "CityCloud";
    private boolean isOpened;
    private String SNAME = "起点";
    private String DNAME = "终点";
    private String CITY = "青岛";
    private double sLat, slng, elat, elng;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_park_detail_layout);
        ButterKnife.inject(this);
        initTitle();
        mActivity = this;
        mContext = this;
        getParkDetailPersenter = new CarPersenter(mContext, (CarConstant.getParkDetailView) mActivity);
        Intent intent = getIntent();
        parkSeq = intent.getStringExtra(StringConstants.PARKSEQ);
        if (EmptyUtils.isNotEmpty(Constants.bdLocation)) {
            latLng = new LatLng(Constants.bdLocation.getLatitude(), Constants.bdLocation.getLongitude());
            CITY = Constants.bdLocation.getCity();
            sLat = Constants.bdLocation.getLatitude();
            slng = Constants.bdLocation.getLongitude();
        }
        getParkDetailPersenter.getParkDetail();
    }

    @OnClick({R.id.btn_pd_navigation})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.btn_pd_navigation:
                //吊起导航
                if (OpenLocalMapUtil.isBaiduMapInstalled()) {
                    openBaiduMap(sLat, slng, SNAME, elat, elng, DNAME, CITY);
                } else {
                    openWebMap(sLat, slng, SNAME, elat, elng, DNAME, CITY);
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        setCTitle("停车场详情");
        setLeftOneBg(R.drawable.ic_back);
    }


    /**
     * 打开百度地图
     */
    private void openBaiduMap(double slat, double slon, String sname, double dlat, double dlon, String dname, String city) {
        if (OpenLocalMapUtil.isBaiduMapInstalled()) {
            try {
                String uri = OpenLocalMapUtil.getBaiduMapUri(String.valueOf(slat), String.valueOf(slon), sname,
                        String.valueOf(dlat), String.valueOf(dlon), dname, city, SRC);
                Intent intent = Intent.parseUri(uri, 0);
                startActivity(intent); //启动调用

                isOpened = true;
            } catch (Exception e) {
                isOpened = false;
                e.printStackTrace();
            }
        } else {
            isOpened = false;
        }
    }

    /**
     * 打开浏览器进行百度地图导航
     */
    private void openWebMap(double slat, double slon, String sname, double dlat, double dlon, String dname, String city) {
        Uri mapUri = Uri.parse(OpenLocalMapUtil.getWebBaiduMapUri(String.valueOf(slat), String.valueOf(slon), sname,
                String.valueOf(dlat), String.valueOf(dlon),
                dname, city, APP_NAME));
        Intent loction = new Intent(Intent.ACTION_VIEW, mapUri);
        startActivity(loction);
    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {
        ProgressDialogHelper.show(mContext);
    }

    @Override
    public void hideLoading() {
        ProgressDialogHelper.dismissDialog();
    }

    @Override
    public void message(String str) {

    }

    @Override
    public String getParkSeq() {
        return parkSeq;
    }

    @Override
    public void onSuccessParkDetail(ParkDetailInfoBean.ParkDetailsInfoBean o) {
        ParkDetailInfoBean.ParkDetailsInfoBean detailBean = o;
        tvParkDetailTitle.setText(detailBean.getParkName());
        tvPdAddress.setText(detailBean.getParkAddress());
        double distance = DistanceUtil.getDistance(latLng, new LatLng(detailBean.getLat(), detailBean.getLng()));
        tvPdDistance.setText((int) distance + "");
        elat = detailBean.getLat();
        elng = detailBean.getLng();
        tvPdFreeCount.setText(detailBean.getVacancyNum() + "");
        tvPdPeople.setText("无");
        tvPdPhoneNumber.setText("无");
        tvPdPrice.setText(detailBean.getPrice() + "");
        tvPdPriceContent.setText(detailBean.getChargeDescription());
        DNAME = detailBean.getParkName();
    }
}
